package cn.com.egova.web.action;

import cn.com.egova.domain.Customer;
import cn.com.egova.domain.Dict;
import cn.com.egova.domain.PageBean;
import cn.com.egova.service.CustomerService;
import cn.com.egova.utils.FastJsonUtil;
import cn.com.egova.utils.UploadUtils;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by yuanchao on 2020/5/23.
 */
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
    private static final long serialVersionUID = 3243543669989234l;
    private static final String MEDIA_DIR = "/Library/tomcat-8.5.55/webapps/upload/";
    private Customer customer = new Customer();
    private CustomerService customerService;
    private Integer pageCode = 1;
    private Integer pageSize = 5;
    private File upload;
    private String uploadFileName;
    private String uploadContentType;

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    public void setPageCode(Integer pageCode) {
        if (pageCode == null) {
            pageCode = 1;
        }
        this.pageCode = pageCode;
    }

    public void setPageSize(Integer pageSize) {
        if (pageSize == null) {
            pageSize = 5;
        }
        this.pageSize = pageSize;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    @Override
    public Customer getModel() {
        return customer;
    }

    public String findByPage() {
        DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
        String custName = customer.getCust_name();
        if (custName != null && !custName.trim().isEmpty()) {
            criteria.add(Restrictions.like("cust_name", "%" + custName + "%"));
        }
        Dict level = customer.getLevel();
        if (level != null && !level.getDict_id().trim().isEmpty()) {
            criteria.add(Restrictions.eq("level.dict_id", level.getDict_id()));
        }
        Dict source = customer.getSource();
        if (source != null && !source.getDict_id().isEmpty()) {
            criteria.add(Restrictions.eq("source.dict_id", source.getDict_id()));
        }
        PageBean<Customer> page = customerService.findByPage(pageCode, pageSize, criteria);
        ValueStack stack = ActionContext.getContext().getValueStack();
        stack.set("page", page);
        return "page";
    }

    public String initAddUI() {
        return "initAddUI";
    }

    /**
     * 带文件上传的保存
     *
     * @return
     * @throws IOException
     */
    public String save() throws IOException {
        if (uploadFileName != null && !uploadFileName.isEmpty()) {
            String uuidName = UploadUtils.getUUIDName(uploadFileName);
            customer.setFilepath(MEDIA_DIR + uuidName);
            File dest = new File(MEDIA_DIR, uuidName);
            FileUtils.copyFile(upload, dest);
        }
        customerService.save(customer);
        return "save";
    }


    public String delete() {
        //查找
        customer = customerService.findById(customer.getCust_id());
        String filepath = customer.getFilepath();
        //删除客户
        customerService.delete(customer);
        //删除附件
        File file = new File(filepath);
        if (file.exists()) {
            file.delete();
        }
        return "delete";
    }

    public String initUpdate() {
        customer = customerService.findById(this.customer.getCust_id());
        return "initUpdate";
    }

    public String update() throws IOException {
        if (uploadFileName != null && !uploadFileName.trim().isEmpty()) {
            //添加了新文件,需删除原文件
            String oldFilepath = customer.getFilepath();
            if (oldFilepath != null && !oldFilepath.trim().isEmpty()) {
                File file = new File(oldFilepath);
                if (file.exists()) {
                    file.delete();
                }
            }

            //上传新文件
            String uuidName = UploadUtils.getUUIDName(uploadFileName);
            customer.setFilepath(MEDIA_DIR + uuidName);
            File dest = new File(MEDIA_DIR, uuidName);
            FileUtils.copyFile(upload, dest);
        }
        //更新客户信息
        customerService.update(customer);
        return "update";
    }


    public String findAll() {
        List<Customer> list = customerService.findAll();
        String jsonString = FastJsonUtil.toJSONString(list);
        FastJsonUtil.writeJson(ServletActionContext.getResponse(), jsonString);
        return NONE;
    }
}
